app.controller('RegionIpWbCtrl', ['$scope','$state', '$stateParams','$http','DiagramService','$anchorScroll','$location','$filter','DataService',RegionIpWbCtrl]);

function RegionIpWbCtrl($scope,$state, $stateParams,$http,DiagramService,$anchorScroll,$location,$filter,DataService){
    $anchorScroll.yOffset = -40; 
    $location.hash();
    $anchorScroll();

    $scope.SAndCGroup = []

    $scope.sfGroup = ['是','否']

    function getCommunity(){
        DataService.restGetApi('communities?filter={"where":{"isdelete":0}}')
        .then(function success (data) {
            $scope.SAndCGroup = data;
            })
    }

    $scope.searchText = ''

    $scope.sortText = 'yycsdm'

    $scope.$watch('searchText',function(newValue,oldValue){
        if(newValue != oldValue){
            var filtList = $filter('filter')(list3,newValue)
            list = $filter('orderBy')(filtList,$scope.sortText)
            list2 = deepCopy(list)
            $scope.pageOption['current'] = 0;
            $scope.getData([1,$scope.pageOption.pageSize])
        }
    })

    $scope.$watch('sortText',function(newValue,oldValue){
        if(newValue != oldValue){
            var filtList = $filter('filter')(list3,$scope.searchText)
            list = $filter('orderBy')(filtList,newValue)
            list2 = deepCopy(list)
            $scope.pageOption['current'] = 0;
            $scope.getData([1,$scope.pageOption.pageSize])
        }
    })

    $scope.clickSort = function(type){
        $scope.sortText = type;
    }

    $scope.hintOption = {
        show:false,
        hinttitle:'',
        hintinfo:'111',
        typeImg:'../img/对号.png',
        btn:'继续添加',
        cancelBtn:'返回首页'
    }

    getCommunity();

    var list = []
    var list2 = []
    var list3 = []

    function getList(){
        DataService.restGetApi('netbars')
        .then(function success (data) {
            list = data;
            list2 = deepCopy(list)
            list3 = deepCopy(list)
            $scope.getData([1,$scope.pageOption.pageSize])
        })
    }
    getList();


    $scope.fresh = function(){
        $scope.form={
            yycsdm:'',   
            yycsmc:'',
            type:'网吧',
            dz:'',
            street:'',
            community:'',
            fzr:'',
            fzrlxdh:'',
            mark:''
        }
        $scope.changeFlag = ''
    }

    $scope.fresh();

    $scope.pushStreet = function(item){
        if(item!=$scope.form.street){
            $scope.form.street = item;
        }
    }

    $scope.checkCommunity = function(item){
        if($scope.form.community==''||$scope.form.community.street!=item.street){
            $scope.form.community = item;
        }
    }

    $scope.submitForm = function(){
        $scope.importForm.yycsmc.$dirty = true;
        $scope.importForm.dz.$dirty = true;
        $scope.importForm.street.$dirty = true;
        $scope.importForm.community.$dirty = true;
        $scope.importForm.yycsdm.$dirty = true;
        if($scope.importForm.yycsdm.$valid&&$scope.importForm.yycsmc.$valid&&$scope.importForm.dz.$valid&&$scope.importForm.street.$valid&&$scope.importForm.community.$valid){
            $scope.form.street = $scope.form.street.street;
            $scope.form.community = $scope.form.community.community;
            DataService.restPostApi('netbars/dateChangeByYycsdm',$scope.form)
            .then(function success (data) {
                $scope.hintOption = {
                    show:true,
                    hinttitle:data.top,
                    hintinfo:'',
                    typeImg:'../img/对号.png',
                    btn:'继续操作',
                    cancelBtn:'返回首页'
                }
                $scope.clickCancel = function(){
                    $state.go("app.region.main")
                    $scope.hintOption.show = false;
                }

                $scope.clickSubmit = function(){
                    $scope.fresh();
                    getList();
                    $scope.importForm.yycsmc.$dirty = false;
                    $scope.importForm.dz.$dirty = false;
                    $scope.importForm.street.$dirty = false;
                    $scope.importForm.community.$dirty = false;
                    $scope.importForm.yycsdm.$dirty = false;
                    $scope.changeFlag = ''
                    $scope.hintOption.show=false;
                }
            })
        }
    }

    $scope.clearInput = function(input){
        $scope.form[input] = ''
    }

    var list = []
    
    $scope.getData = function(item){
        if(item) {
            generateData(item);
        }
    }

    $scope.pageOption = {
        pageSize:10,
        itemCount:3 
    }

    function generateData(item) {
        $scope.pageOption.itemCount = list.length;
        $scope.commList = list.slice(item[0]-1,item[1]);
    }

    $scope.change = function(index){
        $anchorScroll();
        $scope.changeFlag = 'change'
        $scope.form= list2[index]
        $scope.form.street = {community:list2[index].community,street:list2[index].street};
        $scope.form.community = {community:list2[index].community,street:list2[index].street};
    }

    $scope.delete = function(yycsdm){
        if(confirm('确认删除么？')){
            DataService.restPostApi('netbars/deleteByYycsdm',{yycsdm:yycsdm})
            .then(function success (data) {
                $scope.hintOption = {
                    show:true,
                    hinttitle:data.top,
                    hintinfo:'',
                    typeImg:'../img/对号.png',
                    btn:'继续操作',
                    cancelBtn:'返回首页'
                }
                $scope.clickCancel = function(){
                    $state.go("app.region.main")
                    $scope.hintOption.show = false;
                }

                $scope.clickSubmit = function(){
                    getList();
                    $scope.hintOption.show = false;
                }
            })
        }    
    }

}
